{include file="public:header" /}
<link rel="stylesheet" href="__CSS__/shapan.css">
<style>
    #shapan{height:480px;position:relative;overflow:scroll;width:100%;z-index:10;}
</style>
<!--楼栋列表-->
<div class="pad_lr_10" >
    <form name="searchform" action="{:url('index')}" method="get" >
    <table width="100%" cellspacing="0" class="search_form">
        <tbody>
        <tr>
            <td>
            <div class="explain_col">
                <input type="hidden" name="house_id" value="{$house_id}" />
                <input type="hidden" name="menuid" value="{$menuid}" />
                &nbsp;&nbsp;当前编辑是&nbsp;&nbsp;
                <a class="red" title="点击标题可刷新当前页面" href="{:url('index',['house_id'=>$house_id,'menuid'=>$menuid])}"><b>{$house_title}</b></a>&nbsp;&nbsp;楼盘沙盘&nbsp;&nbsp;
                <a class="exit_sub" href="{:url('House/index',['menuid'=>458])}">返回楼盘列表</a>&nbsp;&nbsp;
            </div>
            </td>
        </tr>
        </tbody>
    </table>
    </form>
    <div>
        <div class="clearfix">
        <div class="J_tablelist table_list" style="width:49%;float:left;margin-right:10px;" data-acturi="{:url('ajaxEdit')}">
            <table cellspacing="0" width="100%">
                <thead>
                <tr>
                    <th width=25><input type="checkbox" id="checkall_t" class="J_checkall"></th>
                    <th><span data-tdtype="order_by" data-field="id">ID</span></th>
                    <th align="left">楼栋</th>
                    <th width=80>层数</th>
                    <th align="80">户数</th>
                    <th width="80">楼户配比</th>
                    <th align="150">销售状态</th>
                    <th align="80">状态</th>
                    <th align="180">操作</th>
                </tr>
                </thead>
                <tbody>
                {volist name="list" id="val"}
                <tr>
                    <td align="center">
                        <input type="checkbox" class="J_checkitem" value="{$val.id}">
                    </td>
                    <td align="center">{$val.id}</td>
                    <td align="left">{$val.title}</td>
                    <td align="center">{$val.floor_num}</td>
                    <td align="center">{$val.room_num}</td>
                    <td align="center">{$val.floor_rate}</td>
                    <td align="center">{$val.sale_status}</td>
                    <td align="center"><img data-tdtype="toggle" data-id="{$val.id}" data-field="status" data-value="{$val.status}" src="__IMG__/toggle_{if condition="$val.status eq 1"}enabled{else/}disabled{/if}.gif" /></td>
                    <td align="center" class="point">
                        {php}
                            $selected = 0;
                            $text_str = '添加';
                            $point_pos = '0,0';
                            if(in_array($val['id'],$select_points)){
                                $selected = 1;
                                $text_str = '移除';
                                $point_pos = $init_points[$val['id']];
                            }
                        {/php}
                        <a href="javascript:;" data-select="{$selected}" data-value="{$val.id}" id="shapan_{$val.id}" data-pos="{$point_pos}" data-sale="{$val->getData('sale_status')}" data-title="{$val.title}({$val.sale_status})" onclick="addPosition('{$val.id}','{$val.title}({$val.sale_status})',this)">{$text_str}</a>
                        {:rights($options,$val['id'],$val['title'],$menuid)}
                    </td>
                </tr>
                {/volist}
                </tbody>
            </table>
        </div>
            <div style="float:left;width:49%;">
                <div style="margin-bottom:5px;">
                    &nbsp;&nbsp; <div id="J_list_upload_img" class="btn" onclick="ajaxUpload('J_list_upload_img','J_shapan_img');"><span>上传沙盘图片</span></div>
                    <button class="btn btn_submit" id="save" data-house_id="{$house_id}" data-id="{$points.id}" data-uri="{$uri}" type="button">保存沙盘</button>
                    点击左侧操作栏中的<strong style="color:#ff0000;">添加</strong>按钮可添加楼栋到沙盘图片中
                </div>
                <div class="table_list" id="shapan">
                    <div id="shapan-i">
                        <img id="MapImages" src="{$points.img}" />
                    </div>
                </div>
            </div>
        </div>

    <div class="btn_wrap_fixed">
        <label class="select_all"><input type="checkbox" name="checkall" class="J_checkall">全选/取消</label>
    	<input type="button" class="btn" data-tdtype="batch_action" data-acttype="ajax" data-uri="{:url('delete')}" data-name="id" data-msg="确定要删除所选图片么" value="删除" />
        <div id="pages">{$pages}</div>
    </div>
    </div>
</div>
{include file="public:footer" /}
<script src="__PUBLIC__/js/plugins/fileuploader.js"></script>
<script src="__PUBLIC__/js/plugins/jqmodal.js"></script>
<script>
    $(function(){
        //初始化所有点
        var init_point = '{$points.data}';
        if(init_point){
            init_point = eval('('+init_point+')');
            for(var i in init_point){
                var d = init_point[i],p= d['point'].split(','),left_point=parseInt(p[0]),top_point=parseInt(p[1]);
                sha_idot(d.id, d.title, d.sale,left_point,top_point);
            }
        }

        var param = [];
       $("#save").bind('click',function(){
            var house_id = $(this).data('house_id');
            var img      = $('#MapImages').attr('src'),url=$(this).data('uri'),
                     obj = $(".point").find("a[data-select=1]"),id=$(this).data('id');
           if(obj.length==0){
               $.yingloujie.tip({content:'请先添加楼栋，再保存',icon:'error'});
               return false;
           }else{
               $.yingloujie.tip({content:'数据保存中，请稍候',icon:'alert',time:100000});
               obj.each(function(i,o){
                   var tmp = {};
                   tmp.id = $(o).data('value');
                   tmp.point = $(o).data('pos');
                   tmp.title = $(o).data('title');
                   tmp.sale  = $(o).data('sale');
                   param.push(tmp);
               });
           }
           var data = {
               house_id : house_id,
               img      : img,
               data     : param
           };
           if(id){
               data.id = id;
           }
           $.post(url,data,function(result){
               var res = eval('('+result+')');
               if(res.status == 1){
                   $.yingloujie.tip({content:'沙盘保存成功'});
                   if(res.dialog=='add'){
                       setTimeout(function(){window.location.reload();},1000);
                   }
               }else{
                   $.yingloujie.tip({content:'沙盘保存失败', icon:'error'});
               }
           });
       });
    });
    // 增加一个点
    function addPosition(id,msg,o){
        var a = $(o),status= a.data('sale');
        if(a.attr('data-select') == 0){
            sha_idot(id,msg,status,10,10);
            a.attr('data-select',1);
            a.text('移除');
        }else{
            a.attr('data-select',0);
            pointDel(id);
            a.text('添加');
        }
    }
    // 删除一个点
    function pointDel(id){
        $('#dot_'+id).remove();
        $('#shapan_'+id).attr('data-select',0).text('添加');
        //清理表单项目
    }

    // 显示一个点
    //id:楼栋ID
    //ldmc:楼栋名称
    function sha_idot(id,title,status,left,top){
        $('<a>',{
            id: 'dot_'+id,
            'class': 'sha-dot sha-dot-'+status,
            html: title+'<i></i><b title="close"></b>'
        }).css({
            left: left,
            top: top
        }).jqDrag({
                    attachment:'#shapan-i'
                })
                .on('dragEnd', function (el, l, t) {
                    $('#' + this.id.replace('dot','shapan')).attr('data-pos',l+','+t);
                })
                .appendTo('#shapan-i')
                .find('b').click(function(){ pointDel(id); })
    }
    function ajaxUpload(btn,input){
        //上传图片

        var img_uploader = new qq.FileUploaderBasic({
            allowedExtensions: ['jpg','gif','jpeg','png','swf'],
            button: document.getElementById(btn),
            multiple: false,
            action: "{:url('ajaxUploadImg')}",
            inputName: 'img',
            forceMultipart: true, //用$_FILES

            showMessage: function(message){
                $.yingloujie.tip({content:message, icon:'error'});
            },
            onSubmit: function(id, fileName){
                $('#'+btn).addClass('btn_disabled').find('span').text('上传中，请稍候...');
            },
            onComplete: function(id, fileName, result){
                $('#'+btn).removeClass('btn_disabled').find('span').text('上传沙盘图');
                if(result.status == '1'){
                    $('#'+input).val(result.data);
                    $("#MapImages").attr('src',result.data);
                } else {
                    $.yingloujie.tip({content:result.msg, icon:'error'});
                }
            }
        });
    }
</script>
</body>
</html>
